package com.rmt.www.serviceImpl;

import oracle.net.aso.r;

import com.opensymphony.xwork2.ActionContext;
import com.rmt.www.constant.ArtifactStatus;
import com.rmt.www.dao.FeatureArtifactDao;
import com.rmt.www.dao.FeatureArtifactHistoryDao;
import com.rmt.www.dao.FeatureArtifactRequestDao;
import com.rmt.www.dao.ProjectDao;
import com.rmt.www.dao.UserDao;
import com.rmt.www.service.SREManageFeatureService;
import com.rmt.www.utility.SystemTime;
import com.rmt.www.vo.FeatureArtifact;
import com.rmt.www.vo.FeatureArtifactHistory;
import com.rmt.www.vo.FeatureArtifactRequest;
import com.rmt.www.vo.Project;
import com.rmt.www.vo.User;

public class SREManageFeatureServiceImpl implements SREManageFeatureService {

	private FeatureArtifactDao featureArtifactDao;
	private FeatureArtifactRequestDao featureArtifactRequestDao;
	private FeatureArtifactHistoryDao featureArtifactHistoryDao;
	private UserDao userDao;
	private ProjectDao projectDao;
	
	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public FeatureArtifactRequestDao getFeatureArtifactRequestDao() {
		return featureArtifactRequestDao;
	}

	public void setFeatureArtifactRequestDao(
			FeatureArtifactRequestDao featureArtifactRequestDao) {
		this.featureArtifactRequestDao = featureArtifactRequestDao;
	}

	public FeatureArtifactDao getFeatureArtifactDao() {
		return featureArtifactDao;
	}

	public void setFeatureArtifactDao(FeatureArtifactDao featureArtifactDao) {
		this.featureArtifactDao = featureArtifactDao;
	}

	public void addNewFeature(FeatureArtifactRequest request) {
		boolean isprojectbaselinecreated = projectDao.getProject(request.getPid()).isIsbaselinecreated();
		if (isprojectbaselinecreated)
		{
			featureArtifactRequestDao.insertFeatureArtifactRequest(request);
		}
		else 
		{
			FeatureArtifact fa = new FeatureArtifact();
			fa.setPid(request.getPid());
			fa.setTitle(request.getTitle());
			fa.setVersion(request.getVersion());
			fa.setPriority(request.getPriority());
			fa.setSource(request.getSource());
			fa.setRational(request.getRational());
			fa.setStatus(request.getStatus());
			fa.setDescription(request.getDescription());
			fa.setCost(request.getCost());
			fa.setEffort(request.getEffort());
			fa.setRisk(request.getRisk());
			fa.setUpdatedtimes(0);
			User u=(User)ActionContext.getContext().getSession().get("currentUser");
			fa.setAuthorId(u.getUid());
			int lastinsertid = featureArtifactDao.insertFeatureArtifact(fa);
			
			FeatureArtifactHistory h = new FeatureArtifactHistory();
			h.setPid(request.getPid());
			h.setTitle(request.getTitle());
			h.setVersion(request.getVersion());
			h.setPriority(request.getPriority());
			h.setSource(request.getSource());
			h.setRational(request.getRational());
			h.setStatus(request.getStatus());
			h.setCreatedtime(request.getCreatedtime());
			h.setUpdatedtime(request.getUpdatedtime());
			h.setDescription(request.getDescription());
			h.setCost(request.getCost());
			h.setEffort(request.getEffort());
			h.setRisk(request.getRisk());
			h.setUpdatedtimes(request.getUpdatedtimes());
			h.setAuthorId(request.getAuthorId());
			h.setUpdateUserId(request.getUpdateUserId());
			h.setFeatureartifactid(lastinsertid);
			featureArtifactHistoryDao.insertFeatureArtifactHistory(h);
		}
	}

	public void deleteFeature(int id) {
		FeatureArtifact fa=featureArtifactDao.getFeatureArtifactById(id);
		boolean isprojectbaselinecreated = projectDao.getProject(fa.getPid()).isIsbaselinecreated();
		
		if (isprojectbaselinecreated)
		{
			FeatureArtifactRequest r=new FeatureArtifactRequest();
			r.setFeatureartifactid(id);
		 	r.setPid(fa.getPid());
		 	r.setRequesttype("delete");
		 	r.setTitle(fa.getTitle());
		 	r.setAuthorId(fa.getAuthorId());
		 	r.setCreatedtime(fa.getCreatedtime());
		 	
		 	User u=(User)ActionContext.getContext().getSession().get("currentUser");
		 	r.setUpdateUserId(u.getUid());
		 	
		 	//还要设置updatedtime为当前时间
		 	r.setUpdatedtime(new java.sql.Timestamp(SystemTime.getSystemTime().getTime()));
			
		 	r.setVersion(fa.getVersion());
		 	r.setPriority(fa.getPriority());
		 	r.setSource(fa.getSource());
		 	r.setRational(fa.getRational());
		 	r.setStatus(fa.getStatus());
		 	r.setDescription(fa.getDescription());
		 	r.setEffort(fa.getEffort());
		 	r.setRisk(fa.getRisk());
		 	r.setCost(fa.getCost());
		 	
		 	featureArtifactRequestDao.insertFeatureArtifactRequest(r);
		}
		else 
		{
			//下面把原来的r都改成fa！
			FeatureArtifactHistory h = new FeatureArtifactHistory();
			h.setPid(fa.getPid());
			h.setTitle(fa.getTitle());
			h.setVersion(fa.getVersion());
			h.setPriority(fa.getPriority());
			h.setSource(fa.getSource());
			h.setRational(fa.getRational());
			h.setStatus(ArtifactStatus.DELETED);		//mark as deleted
			h.setCreatedtime(fa.getCreatedtime());
			h.setUpdatedtime(fa.getUpdatedtime());
			h.setDescription(fa.getDescription());
			h.setCost(fa.getCost());
			h.setEffort(fa.getEffort());
			h.setRisk(fa.getRisk());
			h.setUpdatedtimes(fa.getUpdatedtimes()+1);
			h.setAuthorId(fa.getAuthorId());
			h.setUpdateUserId(fa.getUpdateUserId());
			h.setFeatureartifactid(fa.getId());
			featureArtifactHistoryDao.insertFeatureArtifactHistory(h);
			featureArtifactDao.deleteFeatureArtifactById(fa.getId());
		}
	}

	public void updateFeature(FeatureArtifactRequest request) {
		boolean isprojectbaselinecreated = projectDao.getProject(request.getPid()).isIsbaselinecreated();
		if (isprojectbaselinecreated)
		{
			featureArtifactRequestDao.insertFeatureArtifactRequest(request);
		}
		else 
		{
			FeatureArtifact fa = new FeatureArtifact();
			fa.setId(request.getFeatureartifactid());//注意这里，是后来新增的
			fa.setPid(request.getPid());
			fa.setTitle(request.getTitle());
			fa.setVersion(request.getVersion());
			fa.setPriority(request.getPriority());
			fa.setSource(request.getSource());
			fa.setRational(request.getRational());
			fa.setStatus(request.getStatus());
			fa.setDescription(request.getDescription());
			fa.setCost(request.getCost());
			fa.setEffort(request.getEffort());
			fa.setRisk(request.getRisk());
			fa.setUpdatedtimes(request.getUpdatedtimes() + 1);
			fa.setUpdateUserId(request.getUpdateUserId());
			fa.setCreatedtime(request.getCreatedtime());
			fa.setUpdatedtime(request.getUpdatedtime());
			fa.setAuthorId(request.getAuthorId());
			featureArtifactDao.updateFeatureArtifact(fa);
			
			
			FeatureArtifactHistory h = new FeatureArtifactHistory();
			h.setPid(request.getPid());
			h.setTitle(request.getTitle());
			h.setVersion(request.getVersion());
			h.setPriority(request.getPriority());
			h.setSource(request.getSource());
			h.setRational(request.getRational());
			h.setStatus(request.getStatus());
			h.setCreatedtime(request.getCreatedtime());
			h.setUpdatedtime(request.getUpdatedtime());
			h.setDescription(request.getDescription());
			h.setCost(request.getCost());
			h.setEffort(request.getEffort());
			h.setRisk(request.getRisk());
			h.setUpdatedtimes(request.getUpdatedtimes()+1);
			h.setAuthorId(request.getAuthorId());
			h.setUpdateUserId(request.getUpdateUserId());
			h.setFeatureartifactid(request.getFeatureartifactid());
			featureArtifactHistoryDao.insertFeatureArtifactHistory(h);
		}
	}
	
	public FeatureArtifact getFeatureArtifactById(int id) {
		FeatureArtifact artifact=featureArtifactDao.getFeatureArtifactById(id);
		String authorName=userDao.getUser(artifact.getAuthorId()).getName();
		artifact.set_authorname(authorName);
		return artifact;
	}

	public FeatureArtifactHistoryDao getFeatureArtifactHistoryDao() {
		return featureArtifactHistoryDao;
	}

	public void setFeatureArtifactHistoryDao(
			FeatureArtifactHistoryDao featureArtifactHistoryDao) {
		this.featureArtifactHistoryDao = featureArtifactHistoryDao;
	}
}
